import { onMounted, onUnmounted } from 'vue'
import { usePlayerStore } from '@/stores/player'

export function useKeyboardControl() {
  const store = usePlayerStore()

  const handleKeydown = (e: KeyboardEvent) => {
    // 如果正在输入，不处理快捷键
    if (e.target instanceof HTMLInputElement || e.target instanceof HTMLTextAreaElement) {
      return
    }

    switch (e.code) {
      case 'Space':
        e.preventDefault()
        store.togglePlay()
        break
      case 'ArrowLeft':
        if (e.ctrlKey || e.metaKey) {
          e.preventDefault()
          store.prev()
        }
        break
      case 'ArrowRight':
        if (e.ctrlKey || e.metaKey) {
          e.preventDefault()
          store.next()
        }
        break
      case 'ArrowUp':
        if (e.ctrlKey || e.metaKey) {
          e.preventDefault()
          store.setVolume(Math.min(1, store.volume + 0.1))
        }
        break
      case 'ArrowDown':
        if (e.ctrlKey || e.metaKey) {
          e.preventDefault()
          store.setVolume(Math.max(0, store.volume - 0.1))
        }
        break
    }
  }

  onMounted(() => {
    window.addEventListener('keydown', handleKeydown)
  })

  onUnmounted(() => {
    window.removeEventListener('keydown', handleKeydown)
  })
} 